package com.xue.apiautoframework.basecase;
import com.xue.apiautoframework.constant.*;
import com.xue.apiautoframework.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;

import java.util.*;

@Slf4j
public class BaseCase extends AbstractTestNGSpringContextTests {

    public Map<String, String> map;

    @BeforeSuite
    public void init() {
        log.info("=================项目自动化开始====================");
        map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("menuId", "10");
    }

    /**
     * 调用接口
     *
     * @param api   Excel中对应api
     * @param cases Excel中对应cases
     * @return 返回响应体字符串
     */
    public ResponseEntity<?> call(API api, Cases cases) {
        log.info("================开始调用接口==============");
        log.info("================接口编号和接口名称:" + api.getApiNumber() + "==" + api.getApiName() + "=============");
        log.info("================用例编号和用例描述:" + cases.getCaseNumber() + "==" + cases.getCaseDes() + "========");
        if ("get".equalsIgnoreCase(api.getApiReqMethod())) {
            String url = api.getApiUrl();
            return RestTemplateUtils.get(url, map, Object.class);
        } else if ("post".equalsIgnoreCase(api.getApiReqMethod())) {
            if ("json".equalsIgnoreCase(api.getApiSubmitType()) && cases.getDependParams() == null) {
                return RestTemplateUtils.postForJson(api.getApiUrl(), map, cases.getParams(), Object.class);
            } else {
                return RestTemplateUtils.postForFormData(api.getApiUrl(), map, cases.getParams(), Object.class);
            }
        }
        return null;
    }

    /**
     * 执行用例,回写数据到Excel中
     *
     * @param api   Excel中对应api
     * @param cases Excel中对应cases
     * @return 添加数据到回写list,并返回响应结果
     */
    public ResponseEntity<?> execute(API api, Cases cases) {
        ResponseEntity<?> response = call(api, cases);
        String actualRes = response.toString().contains(cases.getExpectResult()) ? "Pass" : "Fail";
        ExcelUtils.addWriteBackDataToList(Integer.parseInt(cases.getSerialNum()), Constant.ACTUAL_WRITER_BACK_CELL_NUM, Objects.requireNonNull(response.getBody()).toString());
        ExcelUtils.addWriteBackDataToList(Integer.parseInt(cases.getSerialNum()), Constant.ASSERT_RESULT_CELL_NUM, actualRes);
        log.info("==============================响应结果:{}", response + "=====================================");
        return response;
    }

    /**
     * 回写结果到Excel中
     *
     * @param responseEntity 响应实体
     * @param cases          用例参数
     */
    public static void execute(ResponseEntity<?> responseEntity, Cases cases){
        String actualRes = responseEntity.toString().contains(cases.getExpectResult()) ? "Pass" : "Fail";
        ExcelUtils.addWriteBackDataToList(Integer.parseInt(cases.getSerialNum()), Constant.ACTUAL_WRITER_BACK_CELL_NUM, Objects.requireNonNull(responseEntity.getBody()).toString());
        ExcelUtils.addWriteBackDataToList(Integer.parseInt(cases.getSerialNum()), Constant.ASSERT_RESULT_CELL_NUM, actualRes);
        log.info("==============================响应结果:{}", responseEntity + "=====================================");
    }

    /**
     * 回写数据,关闭资源
     */
    @AfterSuite
    public void finish() {
        log.info("=============批量回写实际响应结果和执行结果=============");
        ExcelUtils.batchWrite();
        log.info("==============项目自动化结束=========================");
    }
}
